package com.think.demo.base.testIO;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class NewIO1And2 {
	public static void main(String[] args) {
		/*Path 类的一些方法*/
//		testPath();
		/*复制文件*/
//		copyFile();
		/*测试test类中的几个方法*/
//		FileTest();
		/*遍历文件*/
//		visitFile();
		/*测试NewIO的channel和buff读写*/
//		testChannelAndBuffer();

	}

	/**
	 * 测试NewIO的channel和buff读写
	 */
	private static void testChannelAndBuffer() {
		//		InputStream inputStream = NewIO1And2.class.getClassLoader().getResourceAsStream("in.txt");
		File file = new File("in.txt");
		System.out.println(file.exists());
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			FileChannel channel = fileInputStream.getChannel();
			FileChannel outChannel = new FileOutputStream("out.txt").getChannel();
//			MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
			ByteBuffer map = ByteBuffer.allocate(1024);
			while (channel.read(map) != -1) {
				map.flip();
//				outChannel.write(map);
				Charset charset = Charset.forName("UTF-8");
				CharsetDecoder charsetDecoder = charset.newDecoder();

				CharBuffer decode = charsetDecoder.decode(map);
				System.out.println(decode);
				//clear之后所有数据都可以读写，所以以前没被覆盖掉的会输出
				map.clear();
				Charset charset1 = Charset.forName("UTF-8");
				CharsetDecoder charsetDecoder1 = charset1.newDecoder();
				CharBuffer decode1 = charsetDecoder1.decode(map);
				System.err.println("1111:" + decode1);
				map.flip();
				Charset charset2 = Charset.forName("UTF-8");
				CharsetDecoder charsetDecoder2 = charset2.newDecoder();
				CharBuffer decode2 = charsetDecoder2.decode(map);
				System.out.println("2222:" + decode2);

				map.clear();
				//charset.decode(map)会影响map
				System.err.println("666:" + charset.decode(map)+":end");
				map.clear();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 使用新类遍历文件
	 */
	private static void visitFile() {
		try {
			Files.walkFileTree(Paths.get("G:","WORK_SPACE"),new SimpleFileVisitor<Path>(){
				@Override
				public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
					System.out.println("访问子目录之前：" + dir.getFileName());
					return super.preVisitDirectory(dir, attrs);
				}

				@Override
				public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
					System.out.println("正在访问file文件：" + file.getFileName());
					if (file.endsWith("pre-receive.sample")){
						System.err.println("find:" + file.toAbsolutePath());
						return FileVisitResult.TERMINATE;
					}
					return super.visitFile(file, attrs);
				}

				@Override
				public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
					return super.visitFileFailed(file, exc);
				}

				@Override
				public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
					System.out.println("访问子目录之后：" + dir.getFileName());
					return super.postVisitDirectory(dir, exc);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Path 类的一些方法
	 */
	private static void testPath() {
		Path path = Paths.get("..");
		System.out.println(path.getFileName());
		System.out.println(path);
		System.out.println(path.getNameCount());
		System.out.println(path.getRoot());
		Path path1 = Paths.get("G:", "Coding", "tomcat");
		System.out.println(path1);
		System.out.println(path1.getNameCount());
		System.out.println(path1.getParent());
		//不存在的文件
		Path path2 = Paths.get("G:", "testssss");
		System.out.println("111111:" + path2.getFileName());
	}

	/**
	 * 测试test类中的几个方法
	 */
	private static void FileTest() {
		Path pathName = Paths.get("G:", "WORK_SPACE", "demo", "name_rename.test");
		File file = pathName.toFile();
		Path pathNameNew = Paths.get("G:", "WORK_SPACE", "name_rename_new.test");

		file.renameTo(pathNameNew.toFile());
		try {
			//临时文件路劲C:\Users\Administrator\AppData\Local\Temp
			File temp = File.createTempFile("20180522temp", "temp");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 复制文件
	 */
	private static void copyFile() {
		try {
			Files.copy(Paths.get("G:", "WORK_SPACE", "demo", "pom.xml")
					, new FileOutputStream("20180521copy.txt"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
